import os
from flask import Flask
from project.settings import BASE_DIR
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_bootstrap import Bootstrap
from flask_cors import CORS
from flask_redis import FlaskRedis
import logging
from logging import FileHandler
db = SQLAlchemy()
migrate=Migrate()
redis_client = FlaskRedis()

# app=Flask(__name__,instance_relative_config=True,template_folder='../templates',static_folder='../static')
def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True, template_folder='../templates', static_folder='../static')

    if test_config is None:
        CONFIG_PATH=BASE_DIR / 'project/settings.py'
        app.config.from_pyfile(CONFIG_PATH,silent=True)
    else:
        app.config.from_mapping(test_config)
    Bootstrap().init_app(app)
    db.init_app(app)
    CORS().init_app(app)
    migrate.init_app(app,db)
    redis_client.init_app(app)
    #引入blog视图
    app.debug = True
    handler = logging.FileHandler('flask.log', encoding="utf-8")
    app.logger.addHandler(handler)

    from app.blog import views as blog
    app.register_blueprint(blog.user_bp)
    from app.blog import models
    return app